home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / src.zoo / src / bitmapread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-17  |  3.1 KB  |  119 lines

  1. /*                        Copyright (c) 1988 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: bitmapread.c,v 1.1 89/03/17 08:20:52 sau Exp $
  9.     $Source: /m1/mgr.new/src/RCS/bitmapread.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /m1/mgr.new/src/RCS/bitmapread.c,v $$Revision: 1.1 $";
  12.  
  13. #include    "dump.h"
  14. #include    "bitmap.h"
  15. #include    <stdio.h>
  16.  
  17.  
  18. /*
  19.     Read a bitmap.
  20.     Given an open FILE pointer to an file containing a bitmap,
  21.     read the header, malloc() a bitmap, and return the pointer to the
  22.     bitmap.
  23. */
  24. BITMAP *
  25. bitmapread( fp )
  26. register FILE    *fp;
  27. {
  28.     BITMAP        *bp = 0;
  29.     register char    *datap;
  30.     int        h, w, d;    /* height, width, depth of bitmap */
  31.     int        sizefile1;    /* the size of 1 line of the bitmap
  32.                     as stored in a file, in bytes */
  33.     int        sizemem1;    /* the size of 1 line of the bitmap
  34.                     as stored in memory, in bytes */
  35.     long        size1diff = 0;    /* if the file padding is greater than
  36.                     the memory padding, the difference in
  37.                     bytes */
  38.  
  39.     if( bitmaphead( fp, &w , &h, &d, &sizefile1 ) ) {
  40.         sizemem1 = BIT_Size(w, 1, d);
  41.         if( sizefile1 > sizemem1 ) {
  42.             size1diff = sizefile1 - sizemem1;
  43.             sizefile1 = sizemem1;
  44.         }
  45.         if( !(bp = bit_alloc(w, h, NULL_DATA, d)) )
  46.             return  0;
  47.         datap = (char *)BIT_DATA(bp);
  48.  
  49.         /*    The bytes of the bitmap data in the file may have
  50.             different alignments than the bitmap data in memory.
  51.             We read one line at a time in such a way as to get
  52.             the memory alignment needed.
  53.         */
  54.         while( h-- > 0 ) {
  55.             if( fread( datap, sizefile1, 1, fp ) != 1 ) {
  56.                 free( (char *)bp );
  57.                 return  0;
  58.             }
  59.             if( size1diff ) {
  60.                 fseek( fp, size1diff, 1 );
  61.             }
  62.             datap += sizemem1;
  63.         }
  64.     }
  65.     return  bp;
  66. }
  67.  
  68.  
  69. /*
  70.     Write a bitmap.
  71.     Given an open FILE pointer to an file and a pointer to a bitmap,
  72.     write the header and the bitmap.  Return 0 on failure, positive on
  73.     success.
  74. */
  75. int
  76. bitmapwrite( fp, bp, flag )
  77. register FILE    *fp;
  78. BITMAP        *bp;
  79. int flag;            /* 1->8 bit (new), 0->16 bit (old) */
  80.  
  81. {
  82.     register char    *datap;
  83.     register int    w, h, d;
  84.     struct b_header    head;
  85.     struct old_b_header    old_head;
  86.  
  87.     int        sizefile1;    /* the size of 1 line of the bitmap
  88.                     as stored in a file, in bytes */
  89.     int        sizemem1;    /* the size of 1 line of the bitmap
  90.                     as stored in memory, in bytes */
  91.  
  92.     w = BIT_WIDE(bp);
  93.     h = BIT_HIGH(bp);
  94.     d = BIT_DEPTH(bp);
  95.     switch(flag) {
  96.     case NEW_BHDR:
  97.         B_PUTHDR8( &head, w, h, d );
  98.         sizefile1 = B_SIZE8(w, 1, d);
  99.        if( fwrite( (char *)&head, sizeof head, 1, fp ) != 1 )
  100.            return  0;
  101.         break;
  102.     case OLD_BHDR:
  103.         B_PUTOLDHDR( &old_head, w, h );
  104.         sizefile1 = B_SIZE16(w, 1, d);
  105.        if( fwrite( (char *)&old_head, sizeof old_head, 1, fp ) != 1 )
  106.            return  0;
  107.         break;
  108.         }
  109.  
  110.     sizemem1 = BIT_Size(w, 1, d);
  111.     datap = (char *)BIT_DATA(bp);
  112.     while( h-- > 0 ) {
  113.         if( fwrite( datap, sizefile1, 1, fp ) != 1 )
  114.             return  0;
  115.         datap += sizemem1;
  116.     }
  117.     return(1);
  118. }
  119.